#!/bin/bash
wait_client_time=1

if [ -z "${MountPoint}" ]; then
    source ./0_env.sh
fi

# dirPath="$1"
# hardsize="$2"
# hardnum="$3"
# id="$4" # 可能为空，参数 2，3，4 可以换顺序
function create_quota()
{
    if (( $# < 2)); then echo "create_quota: invalid arguments"; return 1; fi
    dirPath="$1"
    hardsize="$2" # 可能为空,参数 2，3，4 可以换顺序
    hardnum="$3" # 可能为空
    id="$4" # 可能为空
    cmd="orcafs-ctl --setquota --dir=${dirPath} ${id} ${hardsize} ${hardnum}"

    echo -e "\t创建配额 ${cmd}"
    ${cmd} > /tmp/cmdout
    if [ -s "/tmp/cmdout" ]; then # 文件存在且非大小非 0
        echo -e -n "\t\t"
        cat /tmp/cmdout
    fi
    rm -f /tmp/cmdout
    sleep ${wait_client_time}
    
    num=$(orcafs-ctl --listquota | grep DIR | grep "${dirPath}" | wc -l)
    if (( ${num} == 0 )); then echo "setquota ${dirPath} error";return 1; fi
}

function remove_quota_dir()
{
    dirPath="$1"
    id="$2"

    cmd="orcafs-ctl --removequota --dir=${dirPath} ${id}"
    echo -e "\t删除配额 ${cmd}"

    ${cmd} # 执行 cmd
    if [ $? -ne 0 ]; then return 1; fi
}

function remove_quota_for_type()
{
    dirPath="$1"
    type="$2"

    TMPFILE="/tmp/tmp_file_remove_quota_for_type"
    orcafs-ctl --listquota --dir=${dirPath} | grep -w "${type}" | awk '{print $3}' > "${TMPFILE}"
    quota_num=$(cat ${TMPFILE} | wc -l)
    if (("${quota_num}" == 0));then return 0; fi;

    if [ "${type}" == "DIR_UID" ]; then
        idType="--uid"
    elif [ "${type}" == "DIR_GID" ]; then
        idType="--gid"
    else
        idType=""
    fi
    
    while read -r id
    do
        if [ "${type}"X == "DIR"X ]; then 
            idstr=""
        else
            idstr="${idType}=${id}"
        fi
        echo -e "\torcafs-ctl --removequota --dir=${dirPath} ${idstr}"
        eval orcafs-ctl --removequota --dir="${dirPath}" "${idstr}" #&> /dev/null
    done < ${TMPFILE}
    rm -f ${TMPFILE}
}

# dirPath="$1"
function remove_all_quota_for_dir()
{
    if (( $# != 1)); then echo "remove_all_quota_for_dir: invalid arguments"; return 1; fi
    dirPath="$1"
    remove_quota_for_type "${dirPath}" "DIR_UID"
    remove_quota_for_type "${dirPath}" "DIR_GID"
    remove_quota_for_type "${dirPath}" "DIR"
    return 0
}

# arg1: dir
function check_usednum()
{
    if (($# < 1)); then echo "Invaild arguemnts."; return 1; fi
    dir=$1
    idStr=$2
    absDir=${MountPoint}${dir}

    if [ -z ${idStr} ];then
        findnum=$(find "${absDir}" | wc -l)
        idType="DIR"
        grepStr="grep -w ${dir}"
    else
        type=$(echo $idStr | awk -F'=' '{print$1}')
        id=$(echo $idStr | awk -F'=' '{print$2}')
        
        if [ ${type} == "--uid" ];then
            idType="DIR_UID"
            findnum=$(find "${absDir}" -uid "${id}" | wc -l)
        else
            idType="DIR_GID"
            findnum=$(find "${absDir}" -gid "${id}" | wc -l)
        fi
        grepStr="grep -w ${id}"
    fi

    usednum=$(orcafs-ctl --listquota | grep -w ${idType} | grep -w "${dir}" | ${grepStr} | awk -F'|' '{print $8}' | awk '{print $1}')
    echo -n -e "\t    compare usednum=${usednum}, findnum=${findnum} ..."
    compare_num ${usednum} ${findnum}
}

# arg1: dir
# arg2: --uid=id or --gid=id
function check_usedsize()
{
    if (($# < 1)); then echo "Invaild arguemnts."; return 1; fi
    dir=$1
    idStr=$2
    absDir=${MountPoint}${dir}

    if [ -z ${idStr} ];then
        dusize=$(du -bs "${absDir}" | awk '{print $1}')
        idType="DIR"
        grepStr="grep -w ${dir}"
    else
        type=$(echo $idStr | awk -F'=' '{print$1}')
        id=$(echo $idStr | awk -F'=' '{print$2}')
        if [ ${type} == "--uid" ];then
            idType="DIR_UID"
        else
            idType="DIR_GID"
        fi
        dusize=$(orcafs-ctl --du  --path="${dir}" ${idStr} | awk '{print $5}')
        grepStr="grep -w ${id}"
    fi

    usedsize=$(orcafs-ctl --listquota | grep -w ${idType} | grep -w ${dir} | ${grepStr} | awk -F'|' '{print $5}' | awk '{print $1}')
    echo -n -e "\t    Compare usedsize=${usedsize}, dusize=${dusize} ..."
    compare_num ${usedsize} ${dusize}
}

# arg1: dir
# arg2: --uid=id or --gid=id
function check_usedsize_and_usednum()
{
    if (($# < 1)); then echo "Invaild arguemnts."; return 1; fi
    echo -e "\t检查用量$1($2)"
    check_usedsize "$1" "$2"
    check_usednum "$1" "$2"
}

function sleep_check_usedsize_and_usednum()
{
    if (($# < 1)); then echo "Invaild arguemnts."; return 1; fi
    sleep 1
    echo -e "\t等待 1 秒后, 检查用量$1($2)"
    check_usedsize "$1" "$2"
    check_usednum "$1" "$2"
}